﻿using FlyingEye.DeviceServices;
using FlyingEye.HttpServices;
using HandyControl.Controls;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Windows;
using MessageBox = HandyControl.Controls.MessageBox;
using Window = System.Windows.Window;

namespace FlyingEye.Views.DeviceAppService
{
    /// <summary>
    /// DeviceClientManagementWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DeviceClientManagementWindow : Window
    {
        private readonly HttpClientService _httpClientService;

        private readonly HttpClientServiceOptions _options;

        private readonly HttpUrlsOptions _httpUrlsOptions;

        private readonly ILogger<DeviceClientManagementWindow> _logger;

        public DeviceClientManagementWindow(
            HttpClientService httpClientService,
            IOptions<HttpClientServiceOptions> options,
            IOptions<HttpUrlsOptions> httpUrlsOptions,
            ILogger<DeviceClientManagementWindow> logger)
        {
            InitializeComponent();
            _httpClientService = httpClientService;
            _options = options.Value;
            _options.ClientId = "DeviceAppServiceApp";
            _httpUrlsOptions = httpUrlsOptions.Value;
            _logger = logger;
        }

        List<DeviceClientResponse>? _clients;

        private async void SearchButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                _clients = await _httpClientService.GetAsync<List<DeviceClientResponse>>(_httpUrlsOptions.DeviceAppServiceBaseUrl
                + "/api/device-app-service/v1.0/device-clients");

                DeviceClientDataGrid.ItemsSource = _clients;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                _logger.LogError(ex, ex.Message);
            }
        }

        private void DeleteButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (_clients != null)
                {
                    if (_clients.Any(item => item.IsChecked))
                    {
                        var result = HandyControl.Controls.MessageBox.Show("你确定要删除吗？", "删除提示", MessageBoxButton.YesNo, MessageBoxImage.Warning);

                        if(result != MessageBoxResult.Yes)
                        {
                            return;
                        }
                    }
                    else
                    {
                        return;
                    }

                    _clients.ForEach(async item =>
                    {
                        if (item.IsChecked)
                        {
                            await _httpClientService.DeleteAsync(_httpUrlsOptions.DeviceAppServiceBaseUrl +
                            "/api/device-app-service/v1.0/device-clients/" + item.Id);
                        }
                    });

                    SearchButton_Click(null!, null!);
                }
            }
            catch (Exception ex)
            {
                HandyControl.Controls.MessageBox.Show(ex.ToString());
                _logger.LogError(ex, ex.Message);
            }
        }

        private async void InsertButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                 await _httpClientService.PostAsync<DeviceClientResponse, Object>(_httpUrlsOptions.DeviceAppServiceBaseUrl
                + "/api/device-app-service/v1.0/device-clients",
                new CreateDeviceClientRequest()
                {
                    Site = int.Parse(SiteTextBox.Text),
                    Id = this.DeviceResourceIdTextBox.Text,
                    ProcessName = this.ProcessNameTextBox.Text
                });

                MessageBox.Show("添加成功！");

                SearchButton_Click(null!, null!);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                _logger.LogError(ex, ex.Message);
            }
        }
    }
}
